package com.lsp.lock.text;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: liusupeng
 * @Date: 2021/6/23 16:17
 */
public class Text {
    public static void main(String[] args) {

        new Thread(() -> run()).start();
        new Thread(() -> run()).start();
        new Thread(() -> run()).start();
        new Thread(() -> run()).start();
        new Thread(() -> run()).start();
    }


    static Lock lock = new ReentrantLock(true);


    public static void run() {

        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "获取了锁");
            TimeUnit.SECONDS.sleep(2);
        } catch (Exception e) {

        } finally {
            System.out.println(Thread.currentThread().getName() + "释放了锁");
            lock.unlock();
        }


    }


}
